import tkinter as tk
from PIL import Image, ImageTk
from tkinter import ttk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
import pandas as pd


class Statistics(tk.Toplevel):
    def __init__(self, parent):
        super().__init__()

        self.parent = parent
        self.title("问卷详情")

        # 获取屏幕的宽和高
        sreen_width = self.winfo_screenwidth()
        screen_height = self.winfo_screenheight()

        # 设置窗口的宽和高
        root_width = int(sreen_width / 2)
        root_height = int(screen_height / 2)

        # 使窗口居中
        self.geometry("{}x{}+{}+{}".format(
            root_width, root_height,
            int((sreen_width - root_width) / 2), int((screen_height - root_height) / 2)
        ))

        # 使用 Pillow 加载图像
        image = Image.open("Image/return.png")
        # 调整图像大小
        new_size = (25, 25)  # 设置新的尺寸 (宽度, 高度)
        resized_image = image.resize(new_size, Image.LANCZOS)
        photo = ImageTk.PhotoImage(resized_image)

        self.back_button = tk.Button(self,image=photo ,text='返回',font=("宋体", 15), compound='left',command=self.back_to_main)
        self.back_button.photo = photo  # 保存图像对象的引用
        self.back_button.place(relx=0.01,rely=0.01,relwidth=0.15,relheight=0.1)

        # 创建一个可滚动的Canvas

        self.canvas = tk.Canvas(self)
        self.scroll_y = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview)
        self.scroll_frame = tk.Frame(self.canvas)

        self.scroll_frame.bind(
            "<Configure>",
            lambda e: self.canvas.configure(
                scrollregion=self.canvas.bbox("all")
            )
        )

        self.canvas.create_window((0, 0), window=self.scroll_frame, anchor="nw")
        self.canvas.configure(yscrollcommand=self.scroll_y.set)

        # 示例问题数据
        questions = [
            {
                "question": "What is the capital of France?",
                "values": [10, 20, 30, 40]
            },
            {
                "question": "Which planet is known t?",
                "values": [15, 25, 35, 45]
            },
            {
                "question": "Who wrote 'To Kill '?",
                "values": [20, 30, 40, 50]
            }
        ]

        # 添加图表
        for i, q in enumerate(questions):
            fig_bar = Figure(figsize=(6, 4), dpi=100)
            ax_bar = fig_bar.add_subplot(121)
            df = pd.DataFrame({'类别': ['A', 'B', 'C', 'D'], '值': q['values']})
            df.plot(kind='bar', x='类别', y='值', ax=ax_bar, legend=False)
            ax_bar.set_title(f"Q{i + 1}: {q['question']}", loc='right', pad=10, y=1.0)

            ax1_bar = fig_bar.add_subplot(122)
            df.set_index('类别')['值'].plot(kind='pie', autopct='%1.1f%%', ax=ax1_bar, legend=False)
            # ax1_bar.set_title(f"Q{i+1}: {q['question']}")

            canvas_bar = FigureCanvasTkAgg(fig_bar, master=self.scroll_frame)
            canvas_bar_widget = canvas_bar.get_tk_widget()
            canvas_bar_widget.pack(side=tk.TOP, fill=tk.BOTH, expand=1)

        # 放置Canvas和Scrollbar
        self.canvas.place(relx=0, rely=0.15, relwidth=0.95, relheight=0.85)
        self.scroll_y.place(relx=0.95, rely=0.15, relwidth=0.05, relheight=0.85)




    def back_to_main(self):
        self.destroy()
        self.parent.deiconify()